package com.cloudfun.campusshare.service.impl;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.UserPreferWashPointDTO;
import com.cloudfun.campusshare.common.model.vo.WashPointForQueryListVO;
import com.cloudfun.campusshare.entity.UserPreferWashPoint;
import com.cloudfun.campusshare.entity.WashPointEntity;
import com.cloudfun.campusshare.repo.UserPreferWashPointRepo;
import com.cloudfun.campusshare.repo.WashPointEntityRepo;
import com.cloudfun.campusshare.service.UserPreferWashPointService;
import com.cloudfun.campusshare.util.BeanCopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by Huqin on 2020/5/22
 */
@Slf4j
@Service
public class UserPreferWashPointServiceImpl implements UserPreferWashPointService {

    @Autowired
    private UserPreferWashPointRepo userPreferWashPointRepo;

    @Autowired
    private WashPointEntityRepo washPointEntityRepo;

    @Override
    public void save(UserPreferWashPointDTO preferWashPointDTO) {
        String userId = preferWashPointDTO.getUserId();
        String washPointId = preferWashPointDTO.getWashPointId();
        if (StringUtils.isBlank(userId)) {
            throw new BusinessException(Code.ERROR, "用户id为空");
        }
        UserPreferWashPoint preferWashPoint = userPreferWashPointRepo.findByUserId(userId);
        if (null != preferWashPoint) {
            // 相等 不做操作
            if (preferWashPoint.getWashPointId().equals(washPointId)) {
                return;
            }
        } else {
            // 创建
            preferWashPoint = new UserPreferWashPoint();
            preferWashPoint.setUserId(userId);
        }
        preferWashPoint.setWashPointId(washPointId);
        userPreferWashPointRepo.save(preferWashPoint);
    }

    @Override
    public void delete(String userId) {
        UserPreferWashPoint preferWashPoint = userPreferWashPointRepo.findByUserId(userId);
        userPreferWashPointRepo.delete(preferWashPoint);
    }

    @Override
    public void deleteByWashPointId(String washPointId) {
        List<UserPreferWashPoint> preferWashPoints = userPreferWashPointRepo.findAllByWashPointId(washPointId);
        if (!CollectionUtils.isEmpty(preferWashPoints)) {
            userPreferWashPointRepo.deleteAll(preferWashPoints);
        }
    }

    @Override
    public String getPreferWashPointId(String userId) {
        UserPreferWashPoint preferWashPoint = userPreferWashPointRepo.findByUserId(userId);
        if (null == preferWashPoint) {
            return null;
        }
        String washPointId = preferWashPoint.getWashPointId();
        if (ALL_WASH_POINT.equals(washPointId)) {
            return null;
        }
        return washPointId;
    }

    @Override
    public WashPointForQueryListVO getPreferWashPoint(String userId) {
        UserPreferWashPoint preferWashPoint = userPreferWashPointRepo.findByUserId(userId);
        if (null == preferWashPoint) {
            return null;
        }
        String washPointId = preferWashPoint.getWashPointId();
        if (ALL_WASH_POINT.equals(washPointId)) {
            WashPointForQueryListVO washPointForQueryListVO = new WashPointForQueryListVO();
            washPointForQueryListVO.setId(ALL_WASH_POINT);
            return washPointForQueryListVO;
        }
        WashPointEntity pointEntity = washPointEntityRepo.findById(washPointId).orElseThrow(() -> new BusinessException(Code.NODATA, "洗衣点不存在"));
        WashPointForQueryListVO res = BeanCopyUtil.copy(pointEntity, WashPointForQueryListVO.class);
        return res;
    }
}
